#coding=utf-8
#from heroes import *
from Ability import *
from Game import *
#from Ui import *
import time

class UnitBase:
    """unit base"""

    g = None
    
    name = ""
    
    hp = 0
    mp = 0
    max_hp = 0
    max_mp = 0
    
    armor = 0
    resistance = 0
    
    speed = 0
    
    exp = 0
    next_exp = 0
    level = 0
    gold = 0

    strength = 0
    intellect = 0

    cur_targets = []
    old_targets = []

    abilities = []
    buffs = []

    cur_ability = None

    is_activing = False
    

    def __init__(self, g, name, max_hp, max_mp, armor, resistance, speed, exp, next_exp, level, gold, strength, intellect, abilities):
        self.g = g
        self.name = name
        self.max_hp = max_hp
        self.max_mp = max_mp
        self.hp = self.max_hp
        self.mp = self.max_mp
        self.armor = armor
        self.resistance = resistance
        self.speed = speed
        self.exp = exp
        self.next_exp = next_exp
        self.level = level
        self.gold = gold
        self.strength = strength
        self.intellect = intellect
        self.abilities = abilities
        for ability in self.abilities:
            if ability.source == None:
                ability.source = self

    def __str__(self):
        return self.name
    
    #def set(self, name, max_hp, max_mp, armor, resistance, speed, exp, next_exp, level, gold, strength, intellect, abilities):
        

    def set_abilities(self, abilities):
        self.abilities = abilities
        
    def set_buffs(self, buffs):
        self.buffs = buffs

    def set_targets(self, targets):
        self.old_targets = self.cur_targets
        self.cur_targets = targets

    def recover_targets(self):
        targets = self.cur_targets
        self.cur_targets = self.old_targets
        self.old_targets = targets

    def set_ability(self, ability):
        self.cur_ability = ability

    def show_abilities(self):
        self.ui.show_list(self.abilities)
        
    def can_act(self):
        if self.hp <= 0:
            return False
        
        if self.is_activing:
            return False
        
        for ability in self.abilities:
            if ability.can_cast():
                return True
        
        return False
    
        
    def pa_action(self):
        for ability in self.abilities:
            ability.pa_action()
    
        buffs = []
        for buff in self.buffs:
            if buff.is_activing:
                buffs.append(buff)
                buff.pa_action()
        self.buffs = buffs
        
        
    def start_ability(self):
        if self.cur_ability.can_cast():
            self.is_activing = True
        
            self.hp -= self.cur_ability.cost_hp
            if self.hp < 0:
                self.hp = 0
            
                self.mp -= self.cur_ability.cost_mp
            if self.mp < 0:
                self.mp = 0
        
            self.cur_ability.start(self.cur_targets)
            
    def stop_ability(self):
        if self.cur_ability != None:
            self.cur_ability.stop()
            self.cur_ability = None
            

    def on_stop_ability(self):
        #if self.cur_ability.cool_down > 0:
            #self.g.cd_queue.add(self.cur_ability)
        self.cur_ability = None
        self.is_activing = False
        
    def add_buff(self, buff):
        if not buff.can_stack:
            buffs = []
            for _buff in self.buffs:
                if _buff.name != buff.name:
                    buffs.append(_buff)
            self.buffs = buffs
        self.buffs.append(buff)
        
    def del_buff(self, buff):
        if buff in self.buffs:
            self.buffs.remove(buff)

    def on_damage(self, ability, damage, rigid_damage):
        real_damage = damage + rigid_damage
        reduce_damage = int((self.armor * 0.06) / (self.armor * 0.06 + 1) * damage)
        real_damage -= reduce_damage

        self.g.ui.output("%s受到了%d点物理伤害\n\n" % (self.name, real_damage))
        time.sleep(self.g.time_delay)
        
        if (ability.name == "快速连踢"):
            if random.randint(0, 99) < 30:
                buff = DamageBuff(self.g, "出血", ability.source, 5, 20, 0, 0, 0, False, GameText(3, [["%s出血了", "%s伤口在流血", "%s的伤口停止出血"]]))
                self.add_buff(buff)
                buff.start(self)
         
        return real_damage

    def on_mdamage(self, ability, mdamage, rigid_mdamage):
        real_mdamage = mdamage + rigid_mdamage
        reduce_damage = int(self.resistance / 100.0 * mdamage)
        real_mdamage -= reduce_damage
        
        self.g.ui.output("%s受到了%d点魔法伤害\n\n" % (self.name, real_mdamage))
        time.sleep(self.g.time_delay)
        return real_mdamage
            
    def on_last_damage(self, ability, all_damage):
        self.hp -= all_damage
        if self.hp < 0:
            self.hp = 0
            self.g.ui.output("%s倒下了\n\n" % (self.name))
            time.sleep(self.g.time_delay)
        return all_damage
        
    

if __name__ == "__main__":
    print("Unit")
